package com.frank.applicationConfig;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.frank.big.util.GzipRequestInterceptor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author 小石潭记
 * @date 2020/10/23 11:38
 * @Description: ${todo}
 */
@Configuration
public class ApplicationConfig implements WebMvcConfigurer {

    /**
     * 这里首先使用 因为两个地方的返回空值处理不一样
     *  redis的配置里面已经配置了
     * @return
     */
    /*@Bean
    @Primary
    public ObjectMapper customObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.findAndRegisterModules();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        objectMapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.registerModule(new JavaTimeModule());
        return objectMapper;
    }*/

    /**
     * 在其他地方使用该objectMapper的时候，指定@Qualifier("kafkaToDataWareObjectMapper")这个即可区分
     *
     * @return
     */
    @Bean
    @Qualifier("kafkaToDataWareObjectMapper")
    public ObjectMapper kafkaToDataWareObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.findAndRegisterModules();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        objectMapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        objectMapper.registerModule(new JavaTimeModule());
        return objectMapper;
    }

    @Bean
    public GzipRequestInterceptor gzipRequestInterceptor() {
        return new GzipRequestInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        /*registry
                .addInterceptor(gzipRequestInterceptor())
                // 这个控制器所有的接口拦截
                .addPathPatterns("/big/**");*/
        WebMvcConfigurer.super.addInterceptors(registry);
    }
}
